package cs.fsu.fsview;

import org.htmlcleaner.TagNode;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


class StoryAdapter extends BaseAdapter{

	private final static String TAG = "cs.fsu.fsuview.StoryAdapter";
	
	class StoryViewHolder{
		TextView header;
		TextView preview;
	}
	 
	private LayoutInflater mInflater;
	TagNode[] storyHeaders, storyPreviews;
	
	public StoryAdapter(){
		Log.i(TAG, "new StoryAdapter Construct Default");
	}
	
	public StoryAdapter(Context context, TagNode[] headers, TagNode[] previews){
		Log.i(TAG, "new StoryAdapter Construct Override");
		mInflater = LayoutInflater.from(context);
		storyHeaders = headers;
		storyPreviews = previews;
	}
	
	@Override
	public int getCount() {
		return storyHeaders.length;
	}

	public String getLink(int id){
		return storyHeaders[id].findElementByName("a", false).getAttributeByName("href");
	}
	
	@Override
	public Object getItem(int arg0) {
		return storyHeaders[arg0].getText();
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.i(TAG, "getting view at " + position);
		
		StoryViewHolder holder;
		
		if(convertView == null){
			convertView = mInflater.inflate(R.layout.list_item, null);
			holder = new StoryViewHolder();
			holder.header = (TextView) convertView.findViewById(R.id.txtStoryHeader);
			holder.preview = (TextView) convertView.findViewById(R.id.txtStoryPreview);
			
			convertView.setTag(holder);
		}
		else{
			holder = (StoryViewHolder) convertView.getTag();
		}
		
		holder.header.setText(storyHeaders[position].getText());
		Log.i(TAG, "header = " + storyHeaders[position].getText());
		 
		holder.preview.setText(StoryActivity.replace(storyPreviews[position].getText() + "", "&nbsp;", ""));
		
		return convertView;
	}	
};